<!DOCTYPE html>
<html>
  <head>
	<meta charset="gbk">
  </head>
  <body>
	<script type="text/javascript" src="lib/store.js"></script> 
	<script type="text/javascript" src="jquery.min.js"></script> 
	<script type="text/javascript" src="manifest.js"></script> 
    <script>
		/* Google Analysis */
		var _gaq = _gaq || [];
		_gaq.push(['_setAccount', 'UA-8606948-8']);
		_gaq.push(['_trackPageview']);

		(function() {
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		})();
		
		/* Show Page Actoin */
		function checkForValidUrl(tabId, changeInfo, tab) {
			if (tab.url.search("weibo.com") > -1) {
			  chrome.pageAction.show(tabId);
			}
		};
		
		/* Send Options */
		function getOptions(callback) {
			var settings = new Store("settings", {});
			var options = settings.toObject();
			callback(options);
		};
		
		/* Send Modules */
		function getModules(callback) {
			callback(manifest);
		};
		
		/* Process the Requests */
		function onRequest(request, sender, callback) {
			if (request.action == 'getOptions') {
				getOptions(callback);
			}
			if (request.action == 'getModules') {
				getModules(callback);
			}
			
			else if (request.action == 'refresh') {
				chrome.windows.getCurrent(function(win) {
					var cwin = win.id;
					chrome.tabs.getAllInWindow(cwin, function(tabs) {
						for (var i = 0; i < tabs.length; i++) {
							var t = tabs[i].url;
							if (t.match('weibo.com')) {
								var tab = tabs[i];
								chrome.tabs.update(tab.id, {url: tab.url, selected: tab.selected}, null);
							}
						}
					});
				});
			}
			else if (request.action == 'redirect' ) {
				chrome.tabs.create({url: request.url, selected: tab.selected}, null);
			}
			else if (request.action == 'notify' ) {
				if ( request.type == 'post') {
					var notification = chrome.extension.getURL("notification/post.html");
					var notification = webkitNotifications.createHTMLNotification(
					  notification 
					);
					notification.show();
				}
				else if ( request.type == 'msg') {
					var notification = chrome.extension.getURL("notification/msg.html");
					var notification = webkitNotifications.createHTMLNotification(
					  notification 
					);
					notification.show();
				}
				else if ( request.type == 'fan') {
					var notification = chrome.extension.getURL("notification/fan.html");
					var notification = webkitNotifications.createHTMLNotification(
					  notification 
					);
					notification.show();
				}
				else if ( request.type == 'comment') {
					var notification = chrome.extension.getURL("notification/comment.html");
					var notification = webkitNotifications.createHTMLNotification(
					  notification 
					);
					notification.show();
				}
				else if ( request.type == 'atme') {
					var notification = chrome.extension.getURL("notification/atme.html");
					var notification = webkitNotifications.createHTMLNotification(
					  notification 
					);
					notification.show();
				}
			}
		};
		chrome.extension.onRequest.addListener(onRequest);
		chrome.tabs.onUpdated.addListener(checkForValidUrl);
    </script>
  </body>
</html>